package dk.modified.reshary;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CustomSpinnerAdapter<T extends IReportCategories> extends ArrayAdapter<T>{

    // Your sent context
    private Context context;
    // Your custom values for the spinner (User)
    private ArrayList<T> values;
    int textViewHeight;
    int padding;
    private boolean emptyRemoved = false;

    public CustomSpinnerAdapter(Context context, int textViewResourceId,
            ArrayList<T> values) {
        super(context, textViewResourceId, values);
        this.context = context;
        this.values = values;
        
        ReportCategories reportCategory = new ReportCategories();
        reportCategory.setId(-1);
        reportCategory.setReason(context.getString(R.string.spinner_prompt));
        reportCategory.setParentid(0);
        
        if(values.get(0).getId() != -1) values.add(0, (T) reportCategory);
 
        //values.add(0, (T) reportCategory);
        textViewHeight = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,45,context.getResources().getDisplayMetrics()));
        padding = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,5,context.getResources().getDisplayMetrics()));
    }

    public int getCount(){
        //Adjust the count based on the removal of the empty item
//        if(emptyRemoved){
//            return values.size();            
//        }
//        return values.size()-1;     
    	return values.size();
    }

    public T getItem(int position){
       return (T) values.get(position);
    }

    public long getItemId(int position){
//        if(emptyRemoved){
//            return position +1;
//        }
        return position;
    }


    // And the "magic" goes here
    // This is for the "passive" state of the spinner
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // I created a dynamic TextView here, but you can reference your own  custom layout for each spinner item
        TextView label = new TextView(context);
        label.setTextColor(Color.BLACK);
        // Then you can get the current item using the values array (Users array) and the current position
        // You can NOW reference each method you has created in your bean object (User class)
        label.setText(values.get(position).getReason());

        // And finally return your dynamic (or custom) view for each spinner item
        return label;
    }

    // And here is when the "chooser" is popped up
    // Normally is the same view, but you can customize it if you want
    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
//    	
//    	if(!emptyRemoved){
//            // Remove the empty item the first time the dropdown is displayed.
//            emptyRemoved = true;
//            // Set to false to prevent auto-selecting the first item after removal.
//            setNotifyOnChange(false);
//            remove(getItem(0));
//            // Set it back to true for future changes.
//            setNotifyOnChange(true);
//        }
    	
    	
        TextView label = new TextView(context);
        label.setTextColor(Color.BLACK);
        label.setText(values.get(position).getReason());
        label.setHeight(textViewHeight);
        label.setPadding(padding, padding, padding, padding);
        label.setGravity(Gravity.CENTER_VERTICAL);
        if(values.get(position).getId() == -1) label.setHeight(0);
        return label;
        
        
        
        
    }
}